import { getArrayItem, setArrayItem } from '@/utils'
import { webconfig } from '@/utils/constant'
import { crops, soils, fields, plantDetails, cropGrowthes, cropGrowthesStd } from '@/api/initial'
const state = {
  crops: getArrayItem('crops', []),
  soils: getArrayItem('soils', []),
  fields: getArrayItem('fields', []),
  plantDetails: getArrayItem('plantDetails', []),
  cropGrowthes: getArrayItem('cropGrowthes', []),
  cropGrowthesStd: getArrayItem('cropGrowthesStd', []),
  title: getArrayItem('title', webconfig.title),
  name: getArrayItem('name', webconfig.name),
  logo: getArrayItem('logo', webconfig.logo),
  loginBg: getArrayItem('loginBg', webconfig.loginBg),
  // icpShow: getArrayItem('icpShow', webconfig.icpShow || 0)
  // icpShow: getArrayItem('icpShow', 0)
}

const mutations = {
  SET_INITIAL: (state, data) => {
    state.title = data.title
    setArrayItem('title', state.title)
    state.name = data.name
    setArrayItem('name', state.name)
    state.logo = data.logo
    setArrayItem('logo', state.logo)
    state.loginBg = data.loginBg
    setArrayItem('loginBg', state.loginBg)
    state.icpShow = data.icpShow
    setArrayItem('icpShow', state.icpShow)
  },
  SET_CROPS: (state, crops) => {
    state.crops = crops
    setArrayItem('crops', state.crops)
  },
  SET_SOILS: (state, soils) => {
    state.soils = soils
    setArrayItem('soils', state.soils)
  },
  SET_FIELDS: (state, fields) => {
    state.fields = fields
    setArrayItem('fields', state.fields)
  },
  SET_PLANT_DETAILS: (state, plantDetails) => {
    state.plantDetails = plantDetails
    setArrayItem('plantDetails', state.plantDetails)
  },
  SET_CROP_GROWTHES: (state, cropGrowthes) => {
    state.cropGrowthes = cropGrowthes
    setArrayItem('cropGrowthes', state.cropGrowthes)
  },
  SET_CROP_GROWTHES_STD: (state, cropGrowthesStd) => {
    state.cropGrowthesStd = cropGrowthesStd
    setArrayItem('cropGrowthesStd', state.cropGrowthesStd)
  }
}

const actions = {
  // user login
  getSoils({ commit }, force = false) {
    const data = getArrayItem('soils', '')
    if (force || !data || data.length === 0) {
      return new Promise((resolve, reject) => {
        soils().then(response => {
          const { data } = response
          commit('SET_SOILS', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  getCrops({ commit }, force = false) {
    const data = getArrayItem('crops', '')
    if (force || !data || data.length === 0) {
      return new Promise((resolve, reject) => {
        crops().then(response => {
          const { data } = response
          commit('SET_CROPS', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  getFields({ commit }, force = false) {
    const data = getArrayItem('fields', '')
    if (force || !data || data.length === 0) {
      return new Promise((resolve, reject) => {
        fields().then(response => {
          const { data } = response
          commit('SET_FIELDS', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  getPlantDetails({ commit }, force = false) {
    const data = getArrayItem('plantDetails', '')
    if (force || !data || data.length === 0) {
      return new Promise((resolve, reject) => {
        plantDetails().then(response => {
          const { data } = response
          commit('SET_PLANT_DETAILS', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  getCropGrowthes({ commit }, force = false) {
    const data = getArrayItem('cropGrowthes', '')
    if (force || !data || data.length === 0) {
      return new Promise((resolve, reject) => {
        cropGrowthes().then(response => {
          const { data } = response
          commit('SET_CROP_GROWTHES', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  getCropGrowthesStd({ commit }, force = false) {
    const data = getArrayItem('cropGrowthesStd', '')
    if (force || !data || data.length === 0) {
      return new Promise((resolve, reject) => {
        cropGrowthesStd().then(response => {
          const { data } = response
          commit('SET_CROP_GROWTHES_STD', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
